Skip to content

Add generated qiskit-pyo3-ffi crate#16145

Open
jakelishman wants to merge 1 commit intoQiskit:mainfrom
jakelishman:c/rust-py
Open

Add generated qiskit-pyo3-ffi crate#16145
jakelishman wants to merge 1 commit intoQiskit:mainfrom
jakelishman:c/rust-py

Conversation

@jakelishman
Copy link
Copy Markdown
Member

@jakelishman jakelishman commented May 5, 2026

This adds logic to our qiskit-bindgen export library to generate raw Rust-space bindings that link against the Qiskit C API using the same PyCapsule-stored vtable system that the qiskit.h header file in QISKIT_PYTHON_EXTENSION mode does.

For the build process, this adds a new make command:

make qiskit-pyo3-ffi

that populates the complete crate into dist/rust. The low-level worker of the make command is the new subcommand

[cargo run -p] qiskit-bindgen-cli -- generate-pyo3 -c crates/cext -o dist/rust/qiskit-pyo3-ffi

I've done this using a partial "template" crate embedded in crates/bindgen (but not within its src tree), which is analogous to how we output the C header files; many of the infrastructure files are hand-written, and only part of the system is generated. I set up the template crate crates/bindgen/pyo3-ffi to be largely sensible with respect to using rust-analyzer-enabled IDEs or clippy commands to check the template code itself.

The template crate is not part of the workspace because it's intended for separate distribution. This commit has all the logic to produce the distribution artefact, but doesn't set up any sort of CD pipeline, because we still have some packaging decisions to make about that.

Close #15985

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

Based on #16144.

@jakelishman jakelishman added this to the 2.5.0 milestone May 5, 2026
@jakelishman jakelishman added on hold Can not fix yet Changelog: None Do not include in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository labels May 5, 2026
@github-project-automation github-project-automation Bot moved this to Ready in Qiskit 2.5 May 5, 2026
@jakelishman jakelishman force-pushed the c/rust-py branch 2 times, most recently from 9861a78 to 178a655 Compare May 5, 2026 21:46
@coveralls
Copy link
Copy Markdown

coveralls commented May 5, 2026

Coverage Report for CI Build 25511180937

Coverage decreased (-0.2%) to 87.397%

Details

  • Coverage decreased (-0.2%) from the base build.
  • Patch coverage: 317 uncovered changes across 4 files (0 of 317 lines covered, 0.0%).
  • 15 coverage regressions across 4 files.

Uncovered Changes

File Changed Covered %
crates/bindgen/src/render/rust.rs 266 0 0.0%
crates/bindgen/src/lib.rs 39 0 0.0%
crates/bindgen/src/simple_ir.rs 7 0 0.0%
crates/bindgen-cli/src/main.rs 5 0 0.0%

Coverage Regressions

15 previously-covered lines in 4 files lost coverage.

File Lines Losing Coverage Coverage
crates/circuit/src/parameter/symbol_expr.rs 8 73.93%
crates/qasm2/src/lex.rs 5 92.29%
crates/circuit/src/parameter/parameter_expression.rs 1 91.04%
crates/qasm2/src/expr.rs 1 93.82%

Coverage Stats

Coverage Status
Relevant Lines: 122410
Covered Lines: 106983
Line Coverage: 87.4%
Coverage Strength: 961236.22 hits per line

💛 - Coveralls

@jakelishman jakelishman removed the on hold Can not fix yet label May 7, 2026
@jakelishman jakelishman marked this pull request as ready for review May 7, 2026 17:18
@jakelishman jakelishman requested a review from a team as a code owner May 7, 2026 17:18
@jakelishman jakelishman requested a review from gadial May 7, 2026 17:18
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

This adds logic to our `qiskit-bindgen` export library to generate
raw Rust-space bindings that link against the Qiskit C API using the
same `PyCapsule`-stored vtable system that the `qiskit.h` header file in
`QISKIT_PYTHON_EXTENSION` mode does.

For the build process, this adds a new `make` command:

```bash
make qiskit-pyo3-ffi
```

that populates the complete crate into `dist/rust`.  The low-level
worker of the `make` command is the new subcommand

```bash
[cargo run -p] qiskit-bindgen-cli -- generate-pyo3 -c crates/cext -o dist/rust/qiskit-pyo3-ffi
```

I've done this using a partial "template" crate embedded in
`crates/bindgen` (but not within its `src` tree), which is analogous to
how we output the C header files; many of the infrastructure files are
hand-written, and only part of the system is generated.  I set up the
template crate `crates/bindgen/pyo3-ffi` to be largely sensible with
respect to using `rust-analyzer`-enabled IDEs or clippy commands to
check the template code itself.

The template crate is not part of the workspace because it's intended
for separate distribution.  This commit has all the logic to produce the
distribution artefact, but doesn't set up any sort of CD pipeline,
because we still have some packaging decisions to make about that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

Support writing Rust/Python extension modules with the raw C API

3 participants